package com.vieewer.btdemo.view.info

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.BATTERY_SERVICE
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import com.vieewer.btdemo.utils.CommonUtils
import java.text.SimpleDateFormat
import java.util.*
import android.os.*

class SugonGlassesInfoManager private constructor() {

    companion object {
        val instance = SingletonHolder.holder
    }

    private object SingletonHolder {
        val holder= SugonGlassesInfoManager()
    }


    var mContext: Context? = null
    val PATH_SYS = "/sys/bus/i2c/drivers/bq27542/6-0055/capacity"
    var mSecondBatteryReceiver : BroadcastReceiver ? = null
    var mListener: ISecondBatteryChange? = null


    fun initManager(ctx : Context) {

        mContext = ctx

        initSecondBattery()

    }


    private fun initSecondBattery() {
        mSecondBatteryReceiver = object : BroadcastReceiver() {

            override fun onReceive(context : Context?, intent : Intent?) {
                if (intent!!.action == "android.intent.action.SECOND_BATTERY_CHANGED") {
                    var level = intent!!.getIntExtra("level",50)
                    var status = intent!!.getIntExtra("status", -1)
                    var isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL
                    var pluginType = intent.getIntExtra("plugged", -1)
                    //Log.i("yjj", "level ,$level, status =$status, isCharging =$isCharging, pluginType =$pluginType")

                    if (mListener != null) {
                        mListener!!.onSecondBatteryChange(level)
                    }

                    //onBatteryLevelChanged(level, pluginType != 0,isCharging)
                }
            }

        }
        startListening()
    }


    fun startListening() {
        var filter = IntentFilter()
        filter.addAction("android.intent.action.SECOND_BATTERY_CHANGED")
        mContext!!.registerReceiver(mSecondBatteryReceiver, filter)
    }


    fun unRegister() {
        mContext!!.unregisterReceiver(mSecondBatteryReceiver)
    }

/*    private fun onBatteryLevelChanged(level: Int, b: Boolean, charging: Boolean) {
        //刷新电量



    }*/



    fun getSensorDirection(angle : Float) : String {

        var text = ""

        if (angle <= 15 || angle >= 345) {
            text = "北"
        } else if (angle > 15 && angle <= 75) {
            text = "东北"
        } else if (angle > 75 && angle <= 105) {
            text = "东"
        } else if (angle > 105 && angle <= 165) {
            text = "东南"
        } else if (angle > 165 && angle <= 195) {
            text = "南"
        } else if (angle > 195 && angle <= 255) {
            text = "西南"
        } else if (angle > 255 && angle <= 285) {
            text = "西"
        } else if (angle > 285 && angle < 345) {
            text = "西北"
        }

        return text
    }

    fun getBatteryInfos() : Int{
        var innerValue = 0
        if (mContext != null) {

            var batteryManager = mContext!!.getSystemService(BATTERY_SERVICE) as BatteryManager
            if (Build.VERSION.SDK_INT >= 21) { //(5.0，L)
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
                innerValue = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) ///当前电量百分比


                Log.i("yjj","charge_counter" +  batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
                        + ", current_average" + batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)
                        + ", current_now" + batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
                        + ", capacity" + batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY))


                Log.i("yjj","BATTERY_PROPERTY_ENERGY_COUNTER" +  batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER)
                        + ", BATTERY_PLUGGED_WIRELESS" + batteryManager.getIntProperty(BatteryManager.BATTERY_PLUGGED_WIRELESS)
                        + ", BATTERY_PLUGGED_AC" + batteryManager.getIntProperty(BatteryManager.BATTERY_PLUGGED_AC)
                        + ", BATTERY_PLUGGED_USB" + batteryManager.getIntProperty(BatteryManager.BATTERY_PLUGGED_USB))



            } else if (Build.VERSION.SDK_INT >= 26) { //(8.0，O)
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS) ///充电状态
                Log.i("yjj", "property_status" + batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS))
            }
        }

        return innerValue

    }




    fun getCurrentTime() : String {
        //设置日期相关
        var date = Date(System.currentTimeMillis())

        var simpleDateFormat = SimpleDateFormat("MM月dd日")
        var monthDay =  simpleDateFormat.format(date)

        var week = CommonUtils.getWeek(SimpleDateFormat("yyyy-MM-dd").format(date))

        return "$monthDay $week"
    }

    fun getCurrentHour() : String {
        //设置日期相关
        var date = Date(System.currentTimeMillis())

        var simpleDateFormat2 = SimpleDateFormat("HH:mm")
        var hourMin =  simpleDateFormat2.format(date)

        return hourMin

    }

    fun setOnSecondBatteryChangeListener(listener: ISecondBatteryChange) {
        mListener = listener
    }


    interface ISecondBatteryChange {

        fun onSecondBatteryChange(battery: Int)

    }


}

